package com.zyq.ssyx.common.result;

import lombok.Data;

/**
 * @author clap of thunder
 * @date 2023-11-15 16:13
 * @description 统一结果返回类，使得所有接口返回的都是统一格式{状态码，状态信息，data}
 */

@Data
public class CommonResponse<T> {
    //状态码
    private Integer code;
    //信息
    private String message;
    //数据
    private T data;

    public CommonResponse() {
    }

    public static<T> CommonResponse build(T data,ResultCodeEnum resultCodeEnum){
        CommonResponse<T> tCommonResponse = new CommonResponse<T>();
        if (null!= data){
            tCommonResponse.setData(data);
        }
        tCommonResponse.setCode(resultCodeEnum.getCode());
        tCommonResponse.setMessage(resultCodeEnum.getMessage());
        return tCommonResponse;
    }

    //成功，返回状态码为200的方法
    public static<T> CommonResponse ok(T data){
        CommonResponse okBuild = build(data,ResultCodeEnum.SUCCESS);
        return  okBuild;
    }

    //成功，返回状态码为200的方法
    public static<T> CommonResponse ok(){
        CommonResponse okBuild = build(null,ResultCodeEnum.SUCCESS);
        return  okBuild;
    }

    //失败，返回状态码为201的方法
    public static<T> CommonResponse fail(T data){
        return  build(data,ResultCodeEnum.FAIL);
    }

    //失败，返回状态码为201的方法
    public static<T> CommonResponse fail(){
        return  build(null,ResultCodeEnum.FAIL);
    }
}
